function [position,isterminal,direction] = StopATPeriapsis(t,S,UTC)
GM_sun=1.327124400*10^(11);     %Sun
GM_earth=3.986004418*10^(5);    %Earth
% % GM_moon = 4.9048695*10^(3);     %Moon
temp = GM_earth/(GM_earth+GM_sun);
%[km^3/s^2] Gravitational Parameters
r12 = 1.4959965e+8;
%[km]Distance between the Earth and the Moon.
TU = 5.0226757e+6;
LU = r12;
VU = LU/TU;
if norm(S(1:3)) > 1-temp
    integrationTime = (10-t)*TU;
    UTC_interest = [UTC(1:5),UTC(6)-integrationTime];
    JD_interest = JulianDate(UTC_interest);
    [~,~,S_eci] = CRTBP2ECI_se(JD_interest,S);
    rdot = dot(S_eci(1:3),S_eci(4:6));
    position = rdot; % The value that we want to be zero
    isterminal = 0;  % Halt integration 
    direction = 0;   % The zero can be approached from either direction

else
    position = 1; % The value that we want to be zero
    isterminal = 100;  % Halt integration 
    direction = 100;   % The zero can be approached from either direction
end


end